package com.zx.mobile.controller;

import com.zx.map.service.NoticeLogService;
import com.zx.map.service.model.NoticeLogModel;
import com.zx.mobile.MobileCommonHelper;
import com.zx.mobile.domain.noticelog.*;
import com.zx.mobile.service.proxy.noticelog.NoticeLogListCommonProxy;
import com.zx.mobile.service.proxy.noticelog.NoticeLogNewCommonProxy;
import com.zx.platform.utils.PmpUtils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 15-7-5 Time: 下午8:09 To
 * change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/mobile/index")
public class MobileInterfaceNoticeController {
    /**
     * log
     */
    private static Logger log = Logger
            .getLogger(MobileInterfaceNoticeController.class);
    @Autowired
    private NoticeLogService noticeLogService;

    /**
     * 实时公告查询
     *
     * @param jsonStr
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/getNewNotice")
    @ResponseBody
    public NoticeLogNewResponse getNewNotice(String jsonStr, Model model,
                                             HttpServletRequest request) {
        NoticeLogNewResponse noticeLogNewResponse = new NoticeLogNewResponse();
        log.info("实时公告查询调用参数:" + jsonStr);
        try {
            // 1、 基础验证
            if (StringUtils.isNotBlank(jsonStr)) {
                // //json信息转请求对象
                NoticeLogNewRequest noticeLogNewRequest = MobileCommonHelper.transFormJson(
                        jsonStr, NoticeLogNewRequest.class);
                String checkValidate = NoticeLogNewCommonProxy.checkNoticeLogRequest(noticeLogNewRequest);
                if (StringUtils.isBlank(checkValidate)) {
                    log.info("实时公告查询参数检查完毕");
                    noticeLogNewResponse = NoticeLogNewCommonProxy.getNoticeLogResponse(noticeLogNewRequest);
                    List<NoticeLogModel> noticeLogModelList = noticeLogService.getListByModel(new NoticeLogModel());
                    for (NoticeLogModel temp : noticeLogModelList) {
                        NoticeLogDomain noticeLogDomain = new NoticeLogDomain();
                        noticeLogDomain.setNoticeContent(temp.getNoticeContent());
                        noticeLogDomain.setNoticeId("NT" + temp.getNoticeId());
                        noticeLogDomain.setNoticeTime(PmpUtils.dateToString(temp.getCreateDate(), "yyyy-MM-dd HH:mm:ss"));
                        noticeLogDomain.setNoticeTitle(temp.getNoticeTitle());
                        noticeLogDomain.setNoticeType(temp.getNoticeType());
                        noticeLogNewResponse.setNoticeLogDomain(noticeLogDomain);
                    }
                } else {
                    log.info("输入参数不合法");
                    noticeLogNewResponse.setReturnCode("0003");
                    noticeLogNewResponse.setReturnMsg(checkValidate);
                }
            } else {
                log.info("输入参数不合法");
                noticeLogNewResponse.setReturnCode("0003");
                noticeLogNewResponse.setReturnMsg("输入参数不合法");
            }
        } catch (Exception e) {
            log.error("系统内部异常", e);
            noticeLogNewResponse.setReturnCode("9999");
            noticeLogNewResponse.setReturnMsg("系统内部异常");
        }
        log.info("实时公告查询调用返回:" + ToStringBuilder.reflectionToString(noticeLogNewResponse));
        return noticeLogNewResponse;
    }


    /**
     * 公告列表查询
     *
     * @param jsonStr
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/getNoticeList")
    @ResponseBody
    public NoticeLogListResponse getNoticeList(String jsonStr, Model model,
                                               HttpServletRequest request) {
        NoticeLogListResponse noticeLogListResponse = new NoticeLogListResponse();
        log.info("公告列表查询调用参数:" + jsonStr);
        try {
            // 1、 基础验证
            if (StringUtils.isNotBlank(jsonStr)) {
                // //json信息转请求对象
                NoticeLogListRequest noticeLogListRequest = MobileCommonHelper.transFormJson(
                        jsonStr, NoticeLogListRequest.class);
                String checkValidate = NoticeLogListCommonProxy.checkNoticeLogRequest(noticeLogListRequest);
                if (StringUtils.isBlank(checkValidate)) {
                    log.info("公告列表查询参数检查完毕");
                    noticeLogListResponse = NoticeLogListCommonProxy.getNoticeLogResponse(noticeLogListRequest);
                    //服务更改
                    //List<NoticeLogModel> noticeLogModelList = noticeLogService.getPageByModel(10,1,new NoticeLogModel());
                    Map map= noticeLogService.getPageByModel(10,1,new com.zx.map.service.domain.NoticeLogDomain());
                    List<NoticeLogModel> noticeLogModelList = (List<NoticeLogModel>) map.get("list");
                    List<NoticeLogDomain> noticeLogDomainList = new ArrayList<NoticeLogDomain>();
                    for (NoticeLogModel temp : noticeLogModelList) {
                        NoticeLogDomain noticeLogDomain = new NoticeLogDomain();
                        noticeLogDomain.setNoticeContent(temp.getNoticeContent());
                        noticeLogDomain.setNoticeId("NT" + temp.getNoticeId());
                        noticeLogDomain.setNoticeTime(PmpUtils.dateToString(temp.getCreateDate(), "yyyy-MM-dd HH:mm:ss"));
                        noticeLogDomain.setNoticeTitle(temp.getNoticeTitle());
                        noticeLogDomain.setNoticeType(temp.getNoticeType());
                        noticeLogDomainList.add(noticeLogDomain);
                    }
                    noticeLogListResponse.setNoticeLogDomainList(noticeLogDomainList);
                } else {
                    log.info("输入参数不合法");
                    noticeLogListResponse.setReturnCode("0003");
                    noticeLogListResponse.setReturnMsg(checkValidate);
                }
            } else {
                log.info("输入参数不合法");
                noticeLogListResponse.setReturnCode("0003");
                noticeLogListResponse.setReturnMsg("输入参数不合法");
            }
        } catch (Exception e) {
            log.error("系统内部异常", e);
            noticeLogListResponse.setReturnCode("9999");
            noticeLogListResponse.setReturnMsg("系统内部异常");
        }
        log.info("公告列表查询调用返回:" + ToStringBuilder.reflectionToString(noticeLogListResponse));
        return noticeLogListResponse;
    }
}
